# -*- coding: iso-8859-15 -*-

#    SuperClipboard is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk

class WindowStaticCopy(gtk.Window):
    
    def __init__(self,copier):
        gtk.Window.__init__(self,type=gtk.WINDOW_TOPLEVEL)
        self.copier = copier
        self.copier.gui.windows.append(self)
        self.set_title("Static Copy")
        self.set_icon_from_file(self.copier.config.path['Ico'])
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        self.connect("delete-event",self.delete_event_callback)
        
        self.accel_group = gtk.AccelGroup()
        self.accel_group.connect_group(65307,0, gtk.ACCEL_VISIBLE, self.delete_by_accel_callback)
        self.add_accel_group(self.accel_group)
                
        self.build_window()
        
    def build_window(self): 
        main_vbox = gtk.VBox(False, 5)
        self.add(main_vbox)
        
        first_vbox = gtk.VBox(False,5)
        main_vbox.pack_start(first_vbox)
        
        self.notebook = gtk.Notebook()
        self.notebook.set_tab_pos(gtk.POS_TOP)
        self.notebook.set_scrollable(True)
        
        scrolled_window = gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
        scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
        scrolled_window.add_with_viewport(self.notebook)
        
        first_vbox.pack_start(scrolled_window)
        
        i=1
        self.list_textview = []
        for text in self.copier.config.staticcopy :
            view = gtk.TextView()
            self.list_textview.append(view)
            view.set_editable(True)
            buffer = gtk.TextBuffer()
            buffer.set_text(text)
            view.set_buffer(buffer)
            
            self.notebook.append_page(view,gtk.Label('Copy ' + str(i)))
            i+=1
        
        second_hbox = gtk.HBox(False,5)
        main_vbox.pack_start(second_hbox,False)
        
        clipboard_button = gtk.Button('Set to Clipboard')
        clipboard_button.connect('clicked',self.set_clipboard_callback)
        second_hbox.pack_start(clipboard_button,False)

        add_button = gtk.Button('Add a static copy')
        add_button.connect("clicked",self.add_button_callback)
        second_hbox.pack_start(add_button,False)
        
        self.remove_button = gtk.Button('Remove static copy')
        self.remove_button.connect('clicked',self.remove_button_callback)
        second_hbox.pack_start(self.remove_button,False)
        
        validate_validate_button = gtk.Button('OK')
        validate_validate_button.connect('clicked',self.validate_callback)

        validate_cancel_button = gtk.Button('Cancel')
        validate_cancel_button.connect('clicked',self.destroy_callback)
        
        second_hbox.pack_start(validate_cancel_button,False)
        second_hbox.pack_start(validate_validate_button,False)
        
        if self.notebook.get_n_pages() == 0:
            self.remove_button.set_sensitive(False)
        
        self.show_all()
    
    def validate_callback(self,element=None):
        tmplist = []
        for index in range(0,self.notebook.get_n_pages()):
            view = self.notebook.get_nth_page(index)
            txt = view.get_buffer()
            startiter, enditer = txt.get_bounds()
            txt = txt.get_text(startiter, enditer)
            tmplist.append(txt)
        self.copier.config.staticcopy = tmplist
        self.copier.savestaticsettings()
        self.destroy_callback()
        
    def add_button_callback(self,element=None):
        view = gtk.TextView()
        self.list_textview.append(view)
        view.set_editable(True)
        buffer = gtk.TextBuffer()
        view.set_buffer(buffer)
        self.notebook.append_page(view,gtk.Label('Copy ' + str(self.notebook.get_n_pages()+1) ))
        self.show_all()
        if self.notebook.get_n_pages() > 0:
            self.remove_button.set_sensitive(True)
        self.notebook.set_current_page(self.notebook.get_n_pages()-1)
        
    def remove_button_callback(self,element=None):
        self.notebook.remove_page(self.notebook.get_current_page())
        if self.notebook.get_n_pages() == 0:
            self.remove_button.set_sensitive(False)
            
        
    def set_clipboard_callback(self,element=None):
        notes_txt = self.notebook.get_nth_page(self.notebook.get_current_page()).get_buffer()
        startiter, enditer = notes_txt.get_bounds()
        self.copier.set_element_to_clipboard(None,notes_txt.get_text(startiter, enditer))
        
    def delete_by_accel_callback(self,accel_group,window,key,modifier):
        self.destroy_callback()
        
    def delete_event_callback(self,*args):
        self.destroy_callback()
    
    def destroy_callback(self,element=None):
        self.copier.gui.windows.remove(self)
        self.destroy()
